<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible"
    content="IE=edge">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>

</html>
<script>
  const arr = [
    {
      name: '张三',
      id: 1,
      pid: ''
    },
    {
      name: '里斯',
      id: 2,
      pid: ''
    },
    {
      name: '张小三',
      id: 3,
      pid: '1'
    },
    {
      name: '里小斯',
      id: 4,
      pid: '2'
    },
    {
      name: '张三三',
      id: 5,
      pid: '1'
    }
  ]

  // 递归函数  放在utils/index.js
  function transListToTree(arr, pid) {
    const newArr = []
    arr.forEach(item => {
      if (item.pid === pid) {
        const children = transListToTree(arr, item.id)
        if (children.length) {
          item.children = children
        }
        newArr.push(item)
      }
    })
    return newArr
  }
  console.log(transListToTree(arr, ''));
</script>